package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.enums.ApplicationTypeEnum;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-05
 * @createTime: 14:51:49
 * 应用模块controller
 */
@Controller
@RequestMapping("/web/admin/application")
public class ApplicationModuleAdminManageAction {

    Logger logger = LoggerFactory.getLogger(ApplicationModuleAdminManageAction.class);

    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;

    /**
     * 页面跳转
     */
    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/pages/admin/application/list");
        //查询跳转类型枚举类list
        JSONArray applicationTypeArray = new JSONArray();
        for(ApplicationTypeEnum applicationTypeEnum : ApplicationTypeEnum.values()){
            JSONObject json = new JSONObject();
            json.put("name",applicationTypeEnum.getName());
            json.put("code",applicationTypeEnum.getCode());
            applicationTypeArray.add(json);
        }
        modelAndView.addObject("applicationTypeArray", applicationTypeArray);
        return modelAndView;
    }

    /**
     * 数据列表接口
     *
     * @param pageNo
     * @param pageSize
     * @param applicationName
     * @return
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(String pageNo, String pageSize, String applicationName, String applicationType) {
        QueryParam queryParam = new QueryParam(pageNo, pageSize);
        if (!StringConverters.null2String(applicationName).equals("")) {
            queryParam.getSqlMap().put("applicationName", applicationName);
        }
        if (!StringConverters.null2String(applicationType).equals("")) {
            queryParam.getSqlMap().put("applicationType", applicationType);
        }
        return applicationModuleBiz.findJSONList(queryParam);
    }

    /**
     * 编辑跳转页
     *
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(String id) {
        ModelAndView mv = new ModelAndView("/pages/admin/application/edit");
        ApplicationModule applicationModule = applicationModuleBiz.findModel(StringConverters.null2Long(id));
        if (null != applicationModule && null != applicationModule.getApplicationTypeEnum()) {
            mv.addObject("type", "APP");
        } else {
            mv.addObject("type", "INNER");
        }
        if (null != applicationModule && null != applicationModule.getIcon()) {
            applicationModule.setIcon(Configuration.getConfigurationByName("hostUrl") + applicationModule.getIcon());
        }
        mv.addObject("application", applicationModule);
        return mv;
    }

    /**
     * 应用保存接口
     *
     * @param applicationModule
     * @param type
     * @return
     */
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public ResultDto addOrUpdate(ApplicationModule applicationModule, String type, MultipartFile file) {
        try {
            return applicationModuleBiz.saveOrupdate(applicationModule, type, file);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return ResultUtil.error();
        }
    }

    /**
     * 获取应用数据
     */
    @RequestMapping("/getApplicationTree")
    @ResponseBody
    public ResultDto getApplicationTree(@RequestParam(value = "sysCode", required = false) String sysCode,
                                        @RequestParam(value = "enterpriseId", required = false) String enterpriseId,
                                        HttpServletRequest request) {
        return applicationModuleBiz.getApplicationTree(sysCode, enterpriseId);
    }

    /**
     * 获取企业-选择框
     */
    @RequestMapping("/getApplicationListMagic")
    @ResponseBody
    public List<SelectVO> getApplicationListMagic(@RequestParam(value = "q", required = false) String q,
                                                  @RequestParam(value = "id", required = false) String id,HttpServletRequest request) {
        List<SelectVO> selectVOS = new ArrayList<>();
        ApplicationModule applicationModule = applicationModuleBiz.findModel(StringConverters.ToLong(id));
        if (applicationModule != null) {
            selectVOS.add(new SelectVO(applicationModule.getApplicationName(), applicationModule.getId() + ""));
        }
        QueryParam queryParam = new QueryParam(1, 20);
        queryParam.getSqlMap().put("magicLike", q);
        queryParam.getSqlMap().put("magicId", id);
        queryParam.getSqlMap().put("enterpriseId", SessionKeyUtils.getSessionEnterprise(request).getId()+"");
        List<ApplicationModule> applicationModules = applicationModuleBiz.findList(queryParam);
        applicationModules.forEach(enterprise1 -> selectVOS.add(new SelectVO(enterprise1.getApplicationName(), enterprise1.getId() + "")));
        return selectVOS;
    }
}
